package com.tanhua.admin.listener;


import com.tanhua.autoconfig.template.AliyunGreenTemplate;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class AuditListener {

    @Autowired
    private AliyunGreenTemplate aliyunGreenTemplate;

    @DubboReference
    private MovementApi movementApi;

//    @RabbitListener(bindings = @QueueBinding(
//            value =@Queue(
//                    value = "tanhua.audit.queue",
//                    durable = "true"
//            ),
//            exchange = @Exchange(
//                    value = "tanhua.audit.exchange",
//                    type = ExchangeTypes.TOPIC
//            ),
//    )    key = "audit.movement"
//    )
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "tanhua.audit.queue",
                        durable = "true"

                ),
                exchange = @Exchange(
                        value ="tanhua.audit.exchange",
                        type = ExchangeTypes.TOPIC

                ),
                key = "audit.movement"
        )
)
    public void audit(String movementId) throws Exception {
        System.out.println("内容审核id"+movementId);
        Movement movement = movementApi.findById(movementId);
        if(movement!=null && movement.getState()==0){
            Map<String, String> stringStringMap = aliyunGreenTemplate.greenTextScan(movement.getTextContent());
            Map imageScan = aliyunGreenTemplate.imageScan(movement.getMedias());
            String suggestion = stringStringMap.get("suggestion");
            String suggestion1 = (String)imageScan.get("suggestion");
            int state =0;
            if("block".equals(suggestion) || "block".equals(suggestion1)){
                state =2;
            }
            if("pass".equals(suggestion) && "pass".equals(suggestion1)){
                state =1;
            }

            movementApi.update(movementId,state);
        }

    }
}
